package com.cdtu.labpreview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.common.UserHolder;
import com.cdtu.labpreview.controller.vo.TalkVo;
import com.cdtu.labpreview.controller.vo.ThinkVo;
import com.cdtu.labpreview.controller.vo.ThinkingAndTipsVo;
import com.cdtu.labpreview.entity.*;
import com.cdtu.labpreview.entity.Class;
import com.cdtu.labpreview.mapper.*;
import com.cdtu.labpreview.service.IThinkingAndTipsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ThinkingAndTipsServiceImpl extends ServiceImpl<ThinkingAndTipsMapper, ThinkingAndTips> implements IThinkingAndTipsService {

    @Autowired
    private ThinkingAndTipsMapper thinkingAndTipsMapper;
    @Autowired
    private ThinkMapper thinkMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private TalkMapper talkMapper;

    @Override
    public Result getThinkingAndTips(int no, int type) {
        if(type==1){//学生
            //根据学号获取班级id
            LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Student::getSno,no);
            Student student = studentMapper.selectOne(lqw);
            long classId = student.getClassId();
            //根据班级id获取对应老师发布的仪器介绍
            LambdaQueryWrapper<ThinkingAndTips> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(ThinkingAndTips::getClassId,classId);
            List<ThinkingAndTips> thinkingAndTips = thinkingAndTipsMapper.selectList(lqw2);


            List<ThinkingAndTipsVo> collect = thinkingAndTips.stream().map(thinkingAndTip -> {
                ThinkingAndTipsVo ttv = new ThinkingAndTipsVo();
                BeanUtils.copyProperties(thinkingAndTip,ttv);
                //获取老师名称
                long tno = thinkingAndTip.getTno();
                LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper=new LambdaQueryWrapper<>();
                teacherLambdaQueryWrapper.eq(Teacher::getTno,tno);
                Teacher teacher = teacherMapper.selectOne(teacherLambdaQueryWrapper);
                String teacherNamename = teacher.getName();
                //获取thinkList
                long thinkingAndTipId = thinkingAndTip.getId();
                LambdaQueryWrapper<Think> lqw3 = new LambdaQueryWrapper<>();
                lqw3.eq(Think::getThinkingAndTipsId, thinkingAndTipId);
                List<Think> thinkList = thinkMapper.selectList(lqw3);
                //转换成thinkVoList
                List<ThinkVo> thinkVoList = thinkList.stream().map(think -> {
                    ThinkVo tv = new ThinkVo();
                    BeanUtils.copyProperties(think, tv);

                    //获取思考题对应的评论
                    long thinkId = think.getId();
                    LambdaQueryWrapper<Talk> talkLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    talkLambdaQueryWrapper.eq(Talk::getThinkId, thinkId);
                    List<Talk> talkList = talkMapper.selectList(talkLambdaQueryWrapper);
                    //获取talkVoList
                    List<TalkVo> talkVoList = talkList.stream().map(talk -> {
                        int sno = talk.getSno();
                        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        studentLambdaQueryWrapper.eq(Student::getSno, sno);
                        Student student2 = studentMapper.selectOne(studentLambdaQueryWrapper);
                        String studentName = student2.getName();
                        TalkVo talkVo = new TalkVo();
                        BeanUtils.copyProperties(talk, talkVo);
                        talkVo.setStudentName(studentName);
                        return talkVo;
                    }).collect(Collectors.toList());
                    tv.setThinkVoList(talkVoList);
                    return tv;
                }).collect(Collectors.toList());

                ttv.setThinkVoList(thinkVoList);
                ttv.setTeacherName(teacherNamename);
                return ttv;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }else {//老师
            LambdaQueryWrapper<ThinkingAndTips> lqw=new LambdaQueryWrapper<>();
            lqw.eq(ThinkingAndTips::getTno,no);
            List<ThinkingAndTips> thinkingAndTips = thinkingAndTipsMapper.selectList(lqw);
            List<ThinkingAndTipsVo> collect = thinkingAndTips.stream().map(thinkingAndTip -> {
                ThinkingAndTipsVo ttv = new ThinkingAndTipsVo();
                BeanUtils.copyProperties(thinkingAndTip,ttv);
                //获取班级名称
                long classId = thinkingAndTip.getClassId();
                Class aClass = classMapper.selectById(classId);
                String cname = aClass.getName();
                //获取thinkList
                long thinkingAndTipId = thinkingAndTip.getId();
                LambdaQueryWrapper<Think> lqw2 = new LambdaQueryWrapper<>();
                lqw2.eq(Think::getThinkingAndTipsId, thinkingAndTipId);
                List<Think> thinkList = thinkMapper.selectList(lqw2);
                //转换成thinkVoList
                List<ThinkVo> thinkVoList = thinkList.stream().map(think -> {
                    ThinkVo tv = new ThinkVo();
                    BeanUtils.copyProperties(think, tv);

                    //获取思考题对应的评论
                    long thinkId = think.getId();
                    LambdaQueryWrapper<Talk> talkLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    talkLambdaQueryWrapper.eq(Talk::getThinkId, thinkId);
                    List<Talk> talkList = talkMapper.selectList(talkLambdaQueryWrapper);
                    //获取talkVoList
                    List<TalkVo> talkVoList = talkList.stream().map(talk -> {
                        int sno = talk.getSno();
                        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
                        studentLambdaQueryWrapper.eq(Student::getSno, sno);
                        Student student = studentMapper.selectOne(studentLambdaQueryWrapper);
                        String studentName = student.getName();
                        TalkVo talkVo = new TalkVo();
                        BeanUtils.copyProperties(talk, talkVo);
                        talkVo.setStudentName(studentName);
                        return talkVo;
                    }).collect(Collectors.toList());
                    tv.setThinkVoList(talkVoList);
                    return tv;
                }).collect(Collectors.toList());

                ttv.setThinkVoList(thinkVoList);
                ttv.setClassName(cname);
                return ttv;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }
    }

    @Override
    public Result addThinking(Think think) {
        int tno = UserHolder.getUser().getUserNo();
        think.setTno(tno);
        int flag = thinkMapper.insert(think);
        if(flag<=0) return Result.fail("添加失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result addThinkingAndTips(ThinkingAndTips thinkingAndTips) {
        int tno = UserHolder.getUser().getUserNo();
        thinkingAndTips.setTno(tno);
        int flag = thinkingAndTipsMapper.insert(thinkingAndTips);
        long id = thinkingAndTips.getId();
        LambdaQueryWrapper<Think> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Think::getTno,tno);
        lqw.eq(Think::getThinkingAndTipsId,0);//还没有绑定的思考题及提示id，即需要添加的
        List<Think> thinks = thinkMapper.selectList(lqw);
        List<Think> thinkList = thinks.stream().map(t -> {
            t.setThinkingAndTipsId(id);
            return t;
        }).collect(Collectors.toList());

        for (int i = 0; i < thinkList.size(); i++) {
            thinkMapper.updateById(thinkList.get(i));
        }

        if(flag<=0) return Result.fail("添加失败！请联系管理员");

        return Result.ok();
    }

    @Override
    public Result updateThinkingAndTips(ThinkingAndTips thinkingAndTips) {
        int flag = thinkingAndTipsMapper.updateById(thinkingAndTips);
        if(flag<=0) return Result.fail("修改失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result deleteThinkingAndTips(long id) {
        int flag = thinkingAndTipsMapper.deleteById(id);
        if(flag<=0) return Result.fail("删除失败！请联系管理员");
        return Result.ok();
    }


}
